﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WebRSSBuilder.Helpers;
using WebRSSBuilder.Web;
using WebRSSBuilder.Controls;
using System.Windows.Browser;
using System.ComponentModel;

namespace WebRSSBuilder.Models
{
    public class HomePageViewModel: INotifyPropertyChanged
    {
       public DelegateCommand Navigate { get; set; }
       private Visibility isAuth;
       private String welcomeText;
       public String WelcomeText
       {
           get
           {
               return this.welcomeText; 
           }
           set
           {
               this.welcomeText = value;
               NotifyPropertyChanged("WelcomeText");
           }
       }
       public Visibility IsAuth
        {
            get
            { return isAuth; }
            set
            { this.isAuth = value; NotifyPropertyChanged("IsAuth"); }
        }
        public HomePageViewModel()
        {
            if (WebContext.Current.User.IsAuthenticated)
            {
                IsAuth = Visibility.Visible;
                WelcomeText = "Please select a file then press Go"; 
            }
            else
            {
                IsAuth = Visibility.Collapsed;
                WelcomeText = "Please login!"; 
            }
            Navigate = new DelegateCommand(ExeNavigate, (obj) => { return true; });
            WebContext.Current.Authentication.LoggedIn += new EventHandler<System.ServiceModel.DomainServices.Client.ApplicationServices.AuthenticationEventArgs>(Authentication_LoggedIn);
            WebContext.Current.Authentication.LoggedOut += new EventHandler<System.ServiceModel.DomainServices.Client.ApplicationServices.AuthenticationEventArgs>(Authentication_LoggedOut);
        }

        void Authentication_LoggedOut(object sender, System.ServiceModel.DomainServices.Client.ApplicationServices.AuthenticationEventArgs e)
        {
            MainPage p = ((App.Current.RootVisual as BusyIndicator).Content as MainPage);
            p.ContentFrame.Content = new Home();
        }

        void Authentication_LoggedIn(object sender, System.ServiceModel.DomainServices.Client.ApplicationServices.AuthenticationEventArgs e)
        {
            MainPage p = ((App.Current.RootVisual as BusyIndicator).Content as MainPage);
            p.ContentFrame.Content = new Home();
        }
        private void ExeNavigate(object param)
        {
            if (param != null)
            {
                File f =param as File;
                MainPage p = ((App.Current.RootVisual as BusyIndicator).Content as MainPage); 
                App.Current.Resources.Remove("File");
                App.Current.Resources.Add("File", f);
                App.Current.Host.NavigationState =  "/ViewRss";
                p.ContentFrame.Content = new ViewRss();
                VisualStateManager.GoToState(p.LinkHome, "InactiveLink", true);
                
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string PropertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));

            }
        }
    }
}
